package com.fs.web.base.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.pojo.IntegralProduct;
import com.fs.common.yzMapper.IntegralProductMapper;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.utils.PageHeper;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 积分商品Controller
 */

@Controller
@Slf4j
@RequestMapping("/console/integral/product")
public class IntegralProductController {

    @Autowired
    IntegralProductMapper integralProductMapper;

    /**
     * 获取全部有效商品
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse getList(HttpServletRequest request){
        try {
            Page page = PageHeper.pageRequest(request);
            IPage<IntegralProduct> iPage = integralProductMapper.getList(page);
            DataGrid list = DataGrid.gridBuilder(iPage);
            return JsonResponse.success(list);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }

    /**
     * 获取全部有效无效已删除商品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/allList")
    public JsonResponse getAllList(HttpServletRequest request){
        try {
            Page page = PageHeper.pageRequest(request);
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            //
            Page<IntegralProduct> productAll = integralProductMapper.findProductAll(page, map);

            // IPage iPage = integralProductMapper.selectPage(page, null);
            DataGrid list = DataGrid.gridBuilder(productAll);
            return JsonResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }


    /**
     * 根据商品id查询商品
     * @param request
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getId")
    public JsonResponse getById(HttpServletRequest request,String productId){
        try {
            IntegralProduct integralProduct = integralProductMapper.selectById(productId);
            return JsonResponse.success(integralProduct);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);


    }




    /**
     * 删除商品/改变商品状态无效
     * @param request
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResponse delete(HttpServletRequest request,String productId){
        try {
            IntegralProduct integralProduct = integralProductMapper.selectById(productId);
            integralProduct.setState(1);
            integralProductMapper.update(integralProduct,null);
            return JsonResponse.success(CodeEnum.EX0);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }


    /**
     * 录入商品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")   // @RequestParam Map<String,Object> params
    public JsonResponse add(HttpServletRequest request){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            IntegralProduct integralProduct = JSON.parseObject(JSON.toJSONString(map), IntegralProduct.class);
            integralProductMapper.insert(integralProduct);
            return JsonResponse.success(CodeEnum.EX0);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }


    /**
     * 修改商品信息
     * @param request
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping("/modify")
    public JsonResponse modify(HttpServletRequest request,String productId) {
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            IntegralProduct old = integralProductMapper.selectById(productId);
            IntegralProduct newBean = JSON.parseObject(JSON.toJSONString(map), IntegralProduct.class);
            BeanUtils.copyProperties(newBean,old,"id");
            integralProductMapper.updateById(old);
            return JsonResponse.success(CodeEnum.EX0);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }

}


